package com.lwq.designpattern.structuralPatterns.proxyPattern.dynamicJDKProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName
 * @Description
 * @Author liwq
 * @Date 2021/4/29 15:48
 * @Version 1.0
 */
public class ProxyFactory {
    //维护一个目标对象
    private Object object;

    public ProxyFactory(Object object) {
        this.object = object;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),
                object.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("执行事务之前");
                        Object returnValue = method.invoke(object, args);
                        System.out.println("执行事务之后");
                        return returnValue;
                    }
                });
    }
}
